package com.authine.cloudpivot.ext.kingdee.work;


import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.ext.dto.kingDee.request.KingDeeBasicRequest;
import com.authine.cloudpivot.ext.dto.voucher.KingDeeBasicStats;
import com.authine.cloudpivot.ext.enums.kingdee.KingDeeBasicStatsEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Slf4j
public class KingDeeBasicStatsWork extends BaseWork {

    /**
     * 根据多个组织名称获取金蝶基础数据
     *
     * @return 金蝶基础数据列表
     */
    public SingleResponse<List<KingDeeBasicStats>> listByCompanyName(KingDeeBasicRequest request) {
        log.info("根据多个组织名称获取金蝶基础数据  request:{}", request);

        Set<String> companyNames = request.getCompanyName();

        Boolean like = request.getLike();

        if (null == request.getLike()) {
            like = true;
        }

        List<KingDeeBasicStats> result = new ArrayList<>();

        if (null == like || like) {
            //模糊 查询符合条件的所有数据 根据 id 排序
            companyNames.forEach(e -> {
                List<KingDeeBasicStats> kingDeeBasicStatsList = kingDeeBasicStatsRepository.findByCompanyNameLike(e);
                if (!CollectionUtils.isEmpty(kingDeeBasicStatsList)) {
                    result.addAll(kingDeeBasicStatsList);
                }
            });

        } else {
            //精准匹配数据
            List<KingDeeBasicStats> kingDeeBasicStatsList = kingDeeBasicStatsRepository.findByCompanyNames(new ArrayList<>(companyNames));
            if (!CollectionUtils.isEmpty(kingDeeBasicStatsList)) {
                result.addAll(kingDeeBasicStatsList);
            }
        }

        SingleResponse singleResponse = SingleResponse.buildSuccess();
        singleResponse.setData(result);

        log.info("根据多个组织名称获取金蝶基础数据  业务逻辑处理结束 <<<");

        return singleResponse;
    }


    /**
     * 保存金蝶基础数据
     *
     * @param kingDeeBasicStats 金蝶基础数据实体
     * @return 保存成功的ID
     */
    public String save(KingDeeBasicStats kingDeeBasicStats) {
        //todo 需要添加事务
        log.info("保存金蝶基础数据  request:{}", kingDeeBasicStats);
        Map<String, Object> data = JSON.parseObject(JSON.toJSONString(kingDeeBasicStats), HashMap.class);

        String id = kingDeeBasicStatsRepository.create(data);
        log.info("保存金蝶基础数据  业务逻辑处理结束 <<<");
        return id;
    }

    /**
     * 修改金蝶基础数据
     *
     * @param param 金蝶基础数据实体
     * @return 修改结果提示信息
     */
    public SingleResponse<Integer> update(KingDeeBasicStats param) {
        log.info("修改金蝶基础数据  request:{}", param);

        //todo 需要添加事务

        KingDeeBasicStats kingDeeBasicStats = kingDeeBasicStatsRepository.findById(param.getId());
        Assert.notNull(kingDeeBasicStats,"未找到需要更新的数据");

        if (StringUtils.isNotBlank(param.getCompany_name())) {
            kingDeeBasicStats.setCompany_name(param.getCompany_name());

        }

        if (StringUtils.isNotBlank(param.getKingdee_code())) {
            kingDeeBasicStats.setKingdee_code(param.getKingdee_code());
        }

        if (null != param.getInfo_type()) {
            kingDeeBasicStats.setInfo_type(param.getInfo_type());
        }

        int ss = kingDeeBasicStatsRepository.updateCompanyAndKingdee(kingDeeBasicStats);

        log.info("修改金蝶基础数据  业务逻辑处理结束 <<<");
        SingleResponse singleResponse = SingleResponse.buildSuccess();
        singleResponse.setData(ss);
        return singleResponse;
    }


    public Boolean checkCodeAndNameExist(String kingdee_code, String company_name, KingDeeBasicStatsEnum info_type) {
        return kingDeeBasicStatsRepository.checkCodeAndNameExist(kingdee_code, company_name, info_type);
    }
}
